﻿using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TNCGameEngine.Content;

namespace SpaceAdventureLib.GamePlay
{
    [Serializable]
    public class Campaign
    {

        #region "Properties"

        private TNCGameEngine.Game game = null;
        public TNCGameEngine.Game Game
        {
            get { return this.game; }
            set { this.game = value; }
        }

        public ContentLoader ContentLoader
        {
            get { return this.game.ContentLoader; }
        }

        public ContentManager ContentManager
        {
            get { return this.game.Content; }
        }

        private List<Mission> missions = null;
        public List<Mission> Missions
        {
            get { return this.missions; }
            set { this.missions = value; }
        }

        private int activeMissionIndex = -1;
        public int ActiveMissionIndex
        {
            get { return this.activeMissionIndex; }
            set { this.activeMissionIndex = value; }
        }
        
        public Mission ActiveMission
        {
            get { return this.IsIndexValid(this.activeMissionIndex, this.missions) ? this.missions[this.activeMissionIndex] : null; }
        }

        #endregion

        #region "Constructors"

        public Campaign(TNCGameEngine.Game game)
        {

            this.game = game;

            this.missions = new List<Mission>();
            this.missions.Add(new Mission(this));
            this.activeMissionIndex = 0;

        }

        #endregion

        #region "Methods"

        public void Initialize()
        {
            this.Initialize(this.activeMissionIndex);
        }

        public void Initialize(int index)
        {            
            if (!this.IsIndexValid(index, this.missions)) { return; }
            this.missions[index].Initialize();
        }

        public List<AssetToLoad> GetAssetsToLoad()
        {
            return this.GetAssetsToLoad(this.activeMissionIndex);
        }

        public List<AssetToLoad> GetAssetsToLoad(int index)
        {
            if (!this.IsIndexValid(index, this.missions)) { return new List<AssetToLoad>(); }
            return this.missions[index].GetAssetsToLoad();
        }

        public void LoadContent(ContentLoader loader)
        {
            this.LoadContent(loader, this.activeMissionIndex);
        }

        public void LoadContent(ContentLoader loader, int index)
        {
            if (!this.IsIndexValid(index, this.missions)) { return; }
            this.missions[index].LoadContent(loader);
        }
        
        public void UnloadContent(SharedContentManager content)
        {
            this.UnloadContent(content, this.activeMissionIndex);
        }

        public void UnloadContent(SharedContentManager content, int index)
        {
            if (!this.IsIndexValid(index, this.missions)) { return; }
            this.missions[index].UnloadContent(content);
        }

        public void Update(GameTime gameTime)
        {
            this.Update(gameTime, this.activeMissionIndex);
        }

        public void Update(GameTime gameTime, int index)
        {
            if (!this.IsIndexValid(index, this.missions)) { return; }
            this.missions[index].Update(gameTime);
        }

        public void Draw(GraphicsDevice g)
        {
            this.Draw(g, this.activeMissionIndex);
        }

        public void Draw(GraphicsDevice g, int index)
        {
            if (!this.IsIndexValid(index, this.missions)) { return; }
            this.missions[index].Draw(g);
        }

        private bool IsIndexValid(int index, IList collection, int offset = 0)
        {
            return (index >= 0) && (index < (collection.Count + offset));
        }
        
        #endregion
        
    }
}
